//
//  OAuthViewControllerTests.swift
//  OAuthTest
//
//  Created by gongtao on 15/3/3.
//  Copyright (c) 2015年 _organizeation. All rights reserved.
//

import UIKit
import XCTest

class OAuthViewControllerTests: XCTestCase {

    override func setUp() {
        super.setUp()
    }
    
    override func tearDown() {
        super.tearDown()
    }

    lazy var OAuthVC:OAuthViewController? = {
        //Test中获得的Bundle不能使用mainBundle
        //Test要获得类/stroyboard等都需要勾选target membership
        let bundle = NSBundle(forClass: OAuthViewControllerTests.self)
        let storyboard = UIStoryboard(name: "Main", bundle: bundle)
        return storyboard.instantiateInitialViewController() as? OAuthViewController
    }()
    
    //测试OAuthVC的根视图类型是UIWebView
    func testOAuthVCRootView() {
        let view = OAuthVC?.view
        XCTAssert(view!.isKindOfClass(UIWebView.self), "OAuthVC的根视图类型不是UIWebView")
    }
    
    //测试OAuthVC的根视图设置OAuthViewController为代理
    func testOAuthVCRootViewDelegate() {
        let view = OAuthVC?.view
        XCTAssert((view as! UIWebView).delegate === OAuthVC, "OAuthVC的根视图没有设置OAuthViewController为代理/没有设置代理")
    }
    
    //跳转链接字符串常量
    let loadingString = "https://api.weibo.com/oauth2/authorize?client_id=2720451414&redirect_uri=http://www.itheima.com"
    let registerString = "http://weibo.cn/dpool/ttt/h5/reg.php?wm=4406&appsrc=4o1TqS&backURL=https%3A%2F%2Fapi.weibo.com%2F2%2Foauth2%2Fauthorize%3Fclient_id%3D2720451414%26response_type%3Dcode%26display%3Dmobile%26redirect_uri%3Dhttp%253A%252F%252Fwww.itheima.com%26from%3D%26with_cookie%3D"
    let loadingSuccessString = "https://api.weibo.com/oauth2/authorize"
    let attorneyString = "http://www.itheima.com/?code=" + "7b54cde6155f23bc189fad24c150c6a7"
    let cancelAttorneyString = "http://www.itheima.com/?error_uri=%2Foauth2%2Fauthorize&error=access_denied&error_description=user%20denied%20your%20request.&error_code=21330"
    let switchUserString = "http://login.sina.com.cn/sso/logout.php?entry=openapi&r=https%3A%2F%2Fapi.weibo.com%2Foauth2%2Fauthorize%3Fclient_id%3D2720451414%26redirect_uri%3Dhttp%3A%2F%2Fwww.itheima.com"
    
    func testContinueAndCode() {
        //加载按钮
        var url = NSURL(string: loadingString)
        var result = OAuthVC!.continueAndCode(url!)
        XCTAssert(result.continuePerform, "应该加载")
        XCTAssertNil(result.code, "不应该有code")
        //注册按钮
        url =  NSURL(string: registerString)
        result = OAuthVC!.continueAndCode(url!)
        XCTAssertFalse(result.continuePerform, "不应该加载")
        XCTAssertNil(result.code, "不应该有code")
        //登陆成功
        url = NSURL(string: loadingSuccessString)
        result = OAuthVC!.continueAndCode(url!)
        XCTAssertTrue(result.continuePerform, "应该加载")
        XCTAssertNil(result.code, "不应该有code")
        //授权回调
        url = NSURL(string: attorneyString)
        result = OAuthVC!.continueAndCode(url!)
        XCTAssertFalse(result.continuePerform, "不应该加载")
        XCTAssert(result.code == "7b54cde6155f23bc189fad24c150c6a7", "code不正确")
        //取消授权
        url = NSURL(string: cancelAttorneyString)
        result = OAuthVC!.continueAndCode(url!)
        XCTAssertFalse(result.continuePerform, "不应该加载")
        XCTAssertNil(result.code, "不应该有code")
        //切换账号
        url = NSURL(string: switchUserString)
        result = OAuthVC!.continueAndCode(url!)
        XCTAssertFalse(result.continuePerform, "不应该加载")
        XCTAssertNil(result.code, "不应该有code")
    }
}
